<?php
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/5
 * Time: 14:45
 */

    class weixinTool{
        var $corpId = "wx751e041cdef857d6";
        var $corpSecret = "";

        //构造函数，获取Access Token
        public function __construct($corpSecret = NULL){
            if($corpSecret){
                $this->corpSecret = $corpSecret;
            }

            $this->lasttime = 0;
            $this->access_token = "";

            //查看缓存是否有该数据，并设置相应值
            $memcache = new Memcached();//创建一个memcached对象
            if(!$memcache->addServer('127.0.0.1', 11211)){
                die ("Could not connect");//连接Memcached服务器
            }

            if($memcache->get('lasttime')){
                $this->lasttime = $memcache->get('lasttime');
                $this->access_token = $memcache->get('access_token');
            }

			if ($this->access_token == "" || time() > ($this->lasttime + 7200)){
                $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$this->corpId."&corpsecret=".$this->corpSecret;
                $res = $this->https_request($url);
                $result = json_decode($res, true);
                //保存到memcache
                $this->access_token = $result["access_token"];
                $this->lasttime = time();
                $memcache->set('lasttime',$this->lasttime);
                $memcache->set('access_token',$this->access_token);

            }
            
        }

        //获取用户基本信息
        public function get_user_info($userid){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&userid=".$userid;
            $res = $this->https_request($url);
            return json_decode($res, true);
        }

        //根据code获取成员信息
        public function get_user_info_by_code($code){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$this->access_token."&code=".$code;
            $res = $this->https_request($url);
            return json_decode($res, true);
        }

        //推送消息
        public function send_message($type,$agentid,$userId='@all',$data){
            $msg = array('touser' =>$userId,'agentid'=>$agentid);
            switch($type)
            {
                case 'textcard':
                    $msg['msgtype'] = 'textcard';
					$msg['textcard'] = array('title'=>$data["title"],'description'=>$data["content"],'url'=>'<a href=\"\"></a>');
echo $data["title"]."<br />";
                    break;
                case 'text':
                    $msg['msgtype'] = 'text';
                    $msg['text'] = array('content' => $data["content"]);
                    $msg['safe'] = 0;
                    break;

            }
            $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$this->access_token;
            return $this->https_request($url, urldecode(json_encode($msg)));
        }

        //https请求（支持GET和POST）
        protected function https_request($url, $data = null){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }

       
    }
?>
